package com.hyl.gulimall.order.config;

import com.hyl.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 配置MQ
 * 交换机、队列、绑定关系
 * @author hyl_marco
 * @data 2022/5/24 - 20:26
 */
@Configuration
public class MyMQConfig {


    /**
     * 1.将Exchange、Binding、Queue加入容器中，会在RabbitMQ中自动创建
     * 2.一旦创建，即使属性发生覆盖，也不会有变化
     * @return
     */
    @Bean
    public Queue orderDelayQueue() {
        //死信队列的属性设置
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", 60000L);
        /**
         * public Queue(String name, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
         *              队列名字        是否持久化       是否排他            是否自动删除          自定义属性
         */
        Queue orderDelayQueue = new Queue("order.delay.queue", true, false, false, arguments);
        return orderDelayQueue;
    }

    @Bean
    public Queue orderReleaseQueue() {
        return new Queue("order.release.order.queue", true, false, false);
    }

    @Bean
    public Queue orderSeckillQueue(){
        return new Queue("order.seckill.order.queue",true,false,false);
    }

    @Bean
    public Exchange orderEventExchange() {
        /**
         * TopicExchange(String name, boolean durable, boolean autoDelete, Map<String, Object> arguments)
         */
        return new TopicExchange("order-event-exchange", true, false);
    }

    @Bean
    public Binding orderCreateBingding() {
        /**
         * public Binding(String destination, Binding.DestinationType destinationType, String exchange, String routingKey, Map<String, Object> arguments)
         */
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    @Bean
    public Binding orderReleaseBingding() {
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    /**
     * 订单释放和库存释放进行绑定，在订单未支付释放后主动向库存发消息解锁库存
     * 防止库存解锁延迟队列比订单释放延迟队列快释放消息的特殊情况，导致
     */
    @Bean
    public Binding orderReleaseOtherBinding(){
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other",
                null);
    }

    @Bean
    public Binding orderSeckillBinding(){
        return new Binding("order.seckill.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.seckill.order",
                null);
    }
}
